<?php
namespace Ekadmin\Controller;

use \Think\Controller;
use Think\Page;

class UserController extends CommonController
{

    public function index()
    {
        $param = I('get.');
        $where['uniacid'] = $this->uniacid;
        if ($param["is_tuan"]) {
            $where["is_tuan"] = $param["is_tuan"];
            $this->is_tuan = $param["is_tuan"];
        }
        if ($param["ismarketing_officer"]) {
            $where["ismarketing_officer"] = $param["ismarketing_officer"];
            $this->ismarketing_officer = $param["ismarketing_officer"];
        }
        
        if ($param["is_zongtuanzhang"]) {
            $where["is_zongtuanzhang"] = $param["is_zongtuanzhang"];
            $this->is_zongtuanzhang = $param["is_zongtuanzhang"];
        }
        if ($param['nickname']) {
            $where['nickname'] = array(
                'like',
                '%' . $param['nickname'] . '%'
            );
        }
        $user = M('user');
        $count = $user->where($where)->count();
        $page = new Page($count);
        $list = $user->where($where)
            ->limit($page->firstRow . ',' . $page->listRows)
            ->order(" id desc")
            ->select();
        $this->assign('page', $page->show());
        $this->assign('count', $count);
        $this->assign('list', $list);
        
        
        $this->display();
    }

    public function edit()
    {
        $data = M('user')->where([
            'id' => I('get.id')
        ])
            ->field()
            ->find();
        $this->assign('data', $data);
        $this->display('form');
    }

    public function del()
    {
        if (IS_POST) {
            $param = I('id');
            if (is_array($param)) {
                $map['id'] = array(
                    'in',
                    $param
                );
            } else {
                $map['id'] = $param;
            }
            $map["uniacid"] = $this->uniacid;
            
            M('user')->where($map)->delete();
            
            ajax_return(0, '删除成功');
        }
    }

    /**
     * *@冻结操作
     */
    public function isstatus()
    {
        if (IS_POST) {
            $param = I('id');
            $rs = M("user")->field('id,status')->find($param);
            $map['status'] = $rs['status'] == 1 ? 2 : 1;
            $map['id'] = $param;
            M('user')->save($map);
            ajax_return(0, '操作成功');
        }
    }

    /**
     * *@取消团长操作
     */
    public function istuan()
    {
        if (IS_POST) {
            $param = I('id');
            /**
             * $rs=M("user")->field('id,is_tuan')->find($param); $map['is_tuan']=$rs['is_tuan']==1? 2:1;团长和非团长之间切换 *
             */
            $map['is_tuan'] = 1;
            $map['id'] = $param;
            M('user')->save($map);
            $data = array(
                "uid" => 0,
                "mobile" => ''
            );
            M("xiaoqu")->where("uid=" . $map['id'])->setField($data);
            ajax_return(0, '操作成功');
        }
    }

    /**
     * *@是否老板操作
     */
    public function isboss()
    {
        if (IS_POST) {
            $param = I('id');
            $rs = M("user")->field('id,is_boss')->find($param);
            $map['is_boss'] = $rs['is_boss'] == 1 ? 2 : 1;
            $map['id'] = $param;
            M('user')->save($map);
            ajax_return(0, '操作成功');
        }
    }

    /**
     * *@是否核销员
     */
    public function ismarketing_officer()
    {
        if (IS_POST) {
            $param = I('id');
            $rs = M("user")->field('id,ismarketing_officer')->find($param);
            $map['ismarketing_officer'] = $rs['ismarketing_officer'] == 1 ? 2 : 1;
            $map['id'] = $param;
            M('user')->save($map);
            ajax_return(0, '操作成功');
        }
    }

    public function update()
    {
        if (IS_POST) {
            $param = I('post.');
            $param["addtime"]=strtotime($param["addtime"]);
            $obj = M('user');
            $param["nickname"]=$param["nickname"];
            $obj->save($param);
            ajax_return(0, '保存成功', U('index'));
        }
    }

    /**
     * *@会员优惠卷列表
     */
    public function quan()
    {
        $param = I('get.');
        $where = " uniacid= ".$this->uniacid;
        if ($param['status']) {
            $where .= " and status=" . $param['status'];
        }
        $this->status = intval($param['status']);
        if ($param['uid']) {
            $where .= " and uid=" . $param['uid'];
        }
        $this->uid = intval($param['uid']);
        if ($param['quanid']) {
            $where .= " and quanid=" . $param['quanid'];
        }
        $this->quanid = intval($param['quanid']);
        $this->info = M("quan")->where('uniacid='.$this->uniacid)->order("id desc")->select();
        $obj = M("member_quan");
        $count = $obj->where($where)->count();
        $page = new Page($count);
        $list = $obj->where($where)
            ->limit($page->firstRow . ',' . $page->listRows)
            ->order('id desc')
            ->select();
        if ($list) {
            for ($i = 0; $i < count($list); $i ++) {
                if ($list[$i]["usedtime"]) {
                    $list[$i]["usedtime"] = date("Y-m-d H:i:s", $list[$i]["usedtime"]);
                } else {
                    $list[$i]["usedtime"] = "-";
                }
                $rs = M("quan")->find($list[$i]["quanid"]);
                switch ($list[$i]["quan_type"]) {
                    case 1:
                        $str = "通用卷";
                        break;
                    case 2:
                        $str = "满减卷";
                        break;
                    case 3:
                        $str = "指定商品卷";
                        break;
                }
                $list[$i]["quaninfo"] = "优惠卷种类：" . $rs["id"] . ",类型:" . $str;
                $rs = M("user")->find($list[$i]["uid"]);
                $list[$i]["nickname"] = $rs["nickname"];
                $list[$i]["acv"] = $rs["head"];
                $list[$i]["name"] = $rs["name"];
                $list[$i]["mobile"] = $rs["mobile"];
            }
        }
        $this->assign('page', $page->show());
        $this->assign('count', $count);
        $this->assign('list', $list);
        $this->display();
    }

    /**
     * *@删除会员优惠卷操作
     */
    public function quandel()
    {
        if (IS_POST) {
            $param = I('id');
            if (is_array($param)) {
                $map['id'] = array('in',$param);
            } else {
                $map['id'] = $param;
            }
            
            $map["uniacid"]=$this->uniacid;
            
            M('member_quan')->where($map)->delete();
            ajax_return(0, '删除成功');
        }
    }

    /**
     * *@会员收藏列表
     */
    public function collections()
    {
        $param = I('get.');
        $where['uniacid'] = $this->uniacid;
        if ($param["goods_id"]) {
            $where["goods_id"] = $param["goods_id"];
        }
        $this->goods_id = $param["goods_id"];
        if ($param["uid"]) {
            $where["uid"] = $param["uid"];
        }
        $this->uid = $param["uid"];
        $user = M('collections');
        $count = $user->where($where)->count();
        $page = new Page($count);
        $list = $user->where($where)
            ->limit($page->firstRow . ',' . $page->listRows)
            ->select();
        if ($list) {
            for ($i = 0; $i < count($list); $i ++) {
                $rs = M("user")->find($list[$i]["uid"]);
                $list[$i]["acv"] = $rs["head"];
                $list[$i]["nickname"] = $rs["nickname"];
                $rs = M("goods")->find($list[$i]["id"]);
                $list[$i]["goodsname"] = "商品id:" . $rs["id"] . ",商品名称：" . $rs["name"];
                $list[$i]["thumb"] = $rs["thumb"];
            }
        }
        $this->assign('page', $page->show());
        $this->assign('count', $count);
        $this->assign('list', $list);
        $this->display();
    }

    /**
     * *@批量删除会员收藏操作
     */
    public function collectionsdel()
    {
        if (IS_POST) {
            $param = I('id');
            if (is_array($param)) {
                $map['id'] = array(
                    'in',
                    $param
                );
            } else {
                $map['id'] = $param;
            }
            
            $map["uniacid"]=$this->uniacid;
            
            M('collections')->where($map)->delete();
            ajax_return(0, '删除成功');
        }
    }

    /**
     * *@供货申请列表
     */
    public function store()
    {
        $where["uniacid"]=$this->uniacid;
        
        $param = I('get.');
        if ($param["name"]) {
            $where["name"] = array("like","%" . $param["name"] . "%");
        }
        if ($param["mobile"]) {
            $where["mobile"] = $param["mobile"];
        }
        if ($param["status"]) {
            $where["status"] = $param["status"];
        }
        $this->status = $param["status"];
        if ($param["uid"]) {
            $where["uid"] = $param["uid"];
        }
        $user = M('store');
        $count = $user->where($where)->count();
        $page = new Page($count);
        $list = $user->where($where)
            ->limit($page->firstRow . ',' . $page->listRows)
            ->select();
        if ($list) {
            for ($i = 0; $i < count($list); $i ++) {
                $rs = M("user")->find($list[$i]["uid"]);
                $list[$i]["acv"] = $rs["head"];
                $list[$i]["nickname"] = $rs["nickname"];
            }
        }
        $this->assign('page', $page->show());
        $this->assign('count', $count);
        $this->assign('list', $list);
        $this->display();
    }

    /**
     * *@申请供应商批量审核同意
     */
    public function store_agree()
    {
        if (IS_POST) {
            $param = I('id');
            if (is_array($param)) {
                $map['id'] = array(
                    "in",
                    $param
                );
            } else {
                $map['id'] = $param;
            }
            $map["uniacid"]=$this->uniacid;
            
            $data = array();
            $data["updatetime"] = date("Y-m-d H:i:s");
            $data["status"] = 2;
            M('store')->where($map)->setField($data);
            $rs = M("store")->where($map)->select();
            if ($rs) {
                for ($i = 0; $i < count($rs); $i ++) {
                    M("user")->where("id=" . $rs[$i]["uid"])->setField("is_boss", 1);
                }
            }
            ajax_return(0, '操作成功');
        }
    }

    /**
     * *@申请供应商申批量审核不同意
     */
    public function store_notagree()
    {
        if (IS_POST) {
            $param = I('id');
            if (is_array($param)) {
                $map['id'] = array(
                    "in",
                    $param
                );
            } else {
                $map['id'] = $param;
            }
            
            $map["uniacid"]=$this->uniacid;
            $data = array();
            $data["updatetime"] = date("Y-m-d H:i:s");
            $data["status"] = 3;
            M('store')->where($map)->setField($data);
            ajax_return(0, '操作成功');
        }
    }

    /**
     * *@申请供应商批量删除
     */
    public function storedel()
    {
        if (IS_POST) {
            $param = I('id');
            if (is_array($param)) {
                $map['id'] = array('in',$param);
            } else {
                $map['id'] = $param;
            }
            M('store')->where($map)->delete();
            ajax_return(0, '删除成功');
        }
    }

    /**
     * *@显示赠送会员列表显示界面
     */
    public function sendmember()
    {
        $param = I('get.');
        $where['uniacid'] = $this->uniacid;
        if ($param['name']) {
            $where['name'] = array(
                'like',
                '%' . $param['name'] . '%'
            );
        }
        if ($param['mobile']) {
            $where['mobile'] = $param['mobile'];
        }
        if ($param['nickname']) {
            $where['nickname'] = array(
                'like',
                '%' . $param['nickname'] . '%'
            );
        }
        $user = M('user');
        $count = $user->where($where)->count();
        $page = new Page($count);
        $list = $user->where($where)
            ->limit($page->firstRow . ',' . $page->listRows)
            ->select();
        $this->assign('page', $page->show());
        $this->assign('count', $count);
        $this->assign('list', $list);
        $this->display();
    }

    /**
     * *@显示赠送优惠券界面
     */
    public function sendquan()
    {
        $id = I('id');
        $this->data = M("user")->find($id);
        if (! $this->data) {
            ajax_return(1, "没有用户信息");
        }
        $this->quan = M("quan")->field("id,tips")
            ->order('id desc')->where("uniacid=".$this->uniacid)
            ->select();
        $this->display();
    }

    /**
     * *@赠送优惠券操作
     */
    public function sendquanhandle()
    {
        $param = I('post.');
        if (! $param['uid']) {
            ajax_return(1, '非法操作');
        } 
        /* $rt=M("quan")->field("id,amount")->where("id=".$param['quanid'])->find();//获取当前选中的优惠券可赠送数量 
         * $count=M("member_quan")->where("quanid=".$param['quanid'])->count();//已赠送的数量 
         * if(($rt["amount"]-$count)<$param['num']){ 
         * ajax_return(1, '当前所选优惠券可赠送数量不足，当前可赠送数量为：'.($rt["amount"]-$count)."个"); 
         * } */
        $rs = M("quan")->where("id=" . $param['quanid']." and uniacid=".$this->uniacid)->find();
        $arr = array(
            "uid" => $param["uid"],
            "quanid" => $param['quanid'],
            "addtime" => time(),
            "reason" => $param['reason'],
            "type" => 2,
            "jiner" => $rs["jiner"],
            "money" => $rs['money'],
            'uniacid'=>$this->uniacid,
            'end_time' => strtotime($param['end_time'])
            
        );
        for ($i = 0; $i < $param['num']; $i ++) {
            M("member_quan")->data($arr)->add();
        }
        ajax_return(0, '赠送成功');
    }

    /**
     * *@会员领取红包记录
     */
    public function hblingqu()
    {
        $user = M('hblingqu');
        $where = "uniacid=".$this->uniacid;
        $count = $user->where($where)->count();
        $page = new Page($count);
        $list = $user->where($where)
            ->limit($page->firstRow . ',' . $page->listRows)
            ->select();
        if ($list) {
            for ($i = 0; $i < count($list); $i ++) {
                $rs = M("user")->find($list[$i]["uid"]);
                $list[$i]["nickname"] = $rs["nickname"];
                $list[$i]["head"] = $rs["head"];
            }
        }
        $this->assign('page', $page->show());
        $this->assign('count', $count);
        $this->assign('list', $list);
        $this->display();
    }

    /**
     * *@红包发团列表
     */
    public function hbftlist()
    {
        $user = M('hongbao');
        $where = "uniacid=".$this->uniacid;
        $count = $user->where($where)->count();
        $page = new Page($count);
        $list = $user->where($where)
            ->limit($page->firstRow . ',' . $page->listRows)
            ->select();
        if ($list) {
            for ($i = 0; $i < count($list); $i ++) {
                $rs = M("user")->find($list[$i]["uid"]);
                $list[$i]["nickname"] = $rs["nickname"];
                $list[$i]["head"] = $rs["head"];
            }
        }
        $this->assign('page', $page->show());
        $this->assign('count', $count);
        $this->assign('list', $list);
        $this->display();
    }

    /**
     * *@是否总团长
     */
    public function is_zongtuanzhang()
    {
        if (IS_POST) {
            $param = I('id');
            $rs = M("user")->field('id,is_zongtuanzhang')->find($param);
            $map['is_zongtuanzhang'] = $rs['is_zongtuanzhang'] == 1 ? 2 : 1;
            $map['id'] = $param;
            M('user')->save($map);
            $arr = array(
                "uid" => $param,
                "addtime" => time(),
                "status" => 1,
                "uniacid"=>$this->uniacid
            );
            M("zongtuanzhang")->data($arr)->add();
            ajax_return(0, '操作成功');
        }
    }
}